// 电影列表内的ajax请求 前后台的通讯

import {
    request
} from "../utils/request.js";

export default {
    //  电影列表的页数
    getMovies: ({
        curPage,
        eachPage
    }) => request({
        // GET获取数据
        type: "GET",
        url: "/api/movies",
        data: {
            curPage,
            eachPage
        },
    }),
    // 拿到电影的id
    getOne: _id => request({
        // GET获取数据
        type: "GET",
        url: `/api/movies/${_id}`,

    }),

    // 通过_id查找整条电影数据
    findOne: async _id =>
        await request({
            type: "GET",
            url: `/api/movies/findOne/${_id}`,
        }),
    // 删除图片
    deleteImg: async ({
            _id,
            url,
            token
        }) =>
        await request({
            type: "DELETE",
            // encodeURIComponent将路径中的/做处理
            url: `/resource/movies/${_id}/imgs/${encodeURIComponent(url)}`,
            data: {
                token
            },
        }),
    // 删除图片
    deletePoster: async ({
            _id,
            url,
            token
        }) =>
        await request({
            type: "DELETE",
            // encodeURIComponent将路径中的/做处理
            url: `/resource/movies/${_id}/moviesPoster/${encodeURIComponent(url)}`,
            data: {
                token
            },
        }),

    //删除电影
    deleteMovie: async (deleteData, _id) => {
        return await request({
            type: 'delete',
            url: `/api/movies/${_id}`,
            data: deleteData,
        })
    },
    //更新影院 
    alterMovie: async (newMovie, _id) => {
        return await request({
            type: "put",
            url: `/api/movies/${_id}`,
            data: newMovie,
        })

    },
    //新增 电影
    addMovie: movie =>
        request({
            //POST 在服务器新增一个资源
            type: "POST",
            url: "/api/movies/add-movie",
            data: movie,
        }),
    // 找到所有电影信息
    select: async data =>
      await  request({
            type: "get",
            url: "/api/movies/select",
            data:data
        }),

}